
/*
*   存一下菜单和选项卡的数据
* */
import {baseUrlAdmin, baseUrlAdminHome} from "../../api_server/common/config";

export default {
  namespaced: true,
  state: {
    menuList: [],
    tabList: [],
    tabActive:{},
  },
  mutations: {
    updateMenuList (state, menuList) {
      state.menuList = menuList
    },
    updateTab(state,tab){
      let index = findIndex(state.tabList,tab.path);
      if(index==-1){
          state.tabList.push(tab)
        }
      state.tabActive=tab
      //固定首页在第一个
      let f=state.tabList.filter(e=>{return e.name=='首页'})
      if(f.length==0){
        state.tabList.unshift({name:'首页',path:baseUrlAdminHome})
      }
    },
    removeTab(state,path){
      if( state.tabList.length<=1||path==baseUrlAdminHome)return;
      if(path){
        let index = findIndex( state.tabList,path);
        state.tabList.splice(index,1);
        state.tabActive=state.tabList[index-1]
      }else{
        state.tabList=state.tabList.slice(0,1);
        state.tabActive=state.tabList[0]
      }
      this.$router.push({path:state.tabActive.path})
    },
    tabActive(state,tab){
      state.tabActive=tab
    },
  }
}
function findIndex(array,path) {
  return array.findIndex((item)=> {
    return item.path === path;
  });
}
